var gotoSearch;
var tips={};
require(['monitor', 'gaodeMap'], function () {

    $('#monitor').monitor({
        canId: '23',
        callback: function (data) {
            loadData(data || {});
        },
        clearData: function () {
            loadData({});
        }
    });

    //天气
    var weatherMap = new AMap.Map('mapContainer', {
        resizeEnable: true,
        view: new AMap.View2D({
            zoom: 13
        })
    });
    // 高德地图对象
    var bus = null;
    var map = new MapGaoDe(107.49023438, 36.24427318, 2);
    var MGeocoder;//地理编码插件

    //左侧树
    commonFun.leftTreeNodeClickObj['carChange'] = function (event, treeId, treeNode) {
        if (treeNode.type == "CAR") {
            $("#licPltNo").html(treeNode.cd);
        }
    };

    function loadData(data) {
        //经纬度
        if (data.loLgt && data.loLtt) {
            if (bus) {
                bus.clear();
            }
            map.setMapLevel(14);
            bus = new Bus(map, '', data.loLgt, data.loLtt);
            bus.setDirection(data.direction);
            bus.show();
            map.mapMoveTo(data.loLgt, data.loLtt);
            //天气
            var lnglatXY = new AMap.LngLat(data.loLgt, data.loLtt);//经纬度
            getWeatherInfo(lnglatXY);
        }

        $('#engineRTMonitorClass').setViewData(data);
        //空调开关
        if ((data.airConditionerSwtc != undefined) && (data.airConditionerSwtc == 1)) {
            $("#airConditionerSwtc").html("开启");
        } else {
            $("#airConditionerSwtc").html("关闭");
        }
        //空调系统故障
        $("#airConditionerError").html(airConditionerErrorResolve(data.proType,(data.airConditionerError == undefined ? "" : data.airConditionerError)));
        //故障等级
        $("#errorLevel").setView((data.errorLevel == undefined ? 0 : data.errorLevel), [
            {value: 1, text: '一级'},
            {value: 2, text: '二级'},
            {value: 3, text: '三级'}
        ], {unMatchConfig: {text: '-'}});
        //空调运行状态
        airConditionerRunStResolve((data.airConditionerRunSt == undefined ? "" : data.airConditionerRunSt));
        //空调风机运行状态
        fanStatusResolve((data.fanStatus == undefined ? "" : data.fanStatus));
        //车内温度
        setTemperature("#temperatureIn", data.temperatureIn);
        //车外温度
        setTemperature("#temperatureOutside", data.temperatureOutside);
        //设定温度
        setTemperature("#temperatureSet", data.temperatureSet);
    }

    /**
     * 根据经纬度信息获取天气信息
     * @param lnglatXY
     */
    function getWeatherInfo(lnglatXY) {
        AMap.service(["AMap.Geocoder"], function () {
            if (!MGeocoder) {
                MGeocoder = new AMap.Geocoder({
                    radius: 1000,
                    extensions: "all"
                });
            }
            //逆地理编码
            MGeocoder.getAddress(lnglatXY, function (status, result) {
                if (status === 'complete' && result.info === 'OK') {
                    var city = result.regeocode.addressComponent.city || result.regeocode.addressComponent.province;
                    AMap.service('AMap.Weather', function () {
                        var weather = new AMap.Weather();

                        weather.getLive(city, function (err, data) {
                            if (!err) {
                                weatherMap.setCity(city);
                                $("#forecasts").html("<div>城市/区：" + data.city + "</div><div>天气：" + data.weather + "</div><div>温度：" + data.temperature + "℃</div><div>发布时间：" + data.reportTime + "</div>")
                            }
                        });
                    });
                }
            });
        });
    }

    //设定温度
    function setTemperature(elm, value) {
        if (value != undefined) {
            if (value <= -30) {
                $(elm).html("-");
            }
            else {
                $(elm).html(value);
            }
        }
        else {
            $(elm).html("-");
        }
    }

    //解析空调系统故障状态
    function airConditionerErrorResolve(proType,airConditionerError) {
        var html = "";
        // var tips = [
        //     "空调压力2故障",
        //     "空调压力1故障",
        //     "空调系统电源低压",
        //     "空调系统电源过压",
        //     "蒸发器传感器2断路",
        //     "蒸发器传感器1断路",
        //     "保留",
        //     "回风传感器断路"
        // ];
        var faults=tips[proType];
        for (var i = 0; i < airConditionerError.length; i++) {
            if (airConditionerError[i] != "0") {
                html += getTipStr(faults, ''+(i + 1))+ ",";
            }
        }
        if (html.length > 0) {
            html = html.substring(0, html.length - 1);
        }
        return html;
    }

    //解析空调风机运行状态
    function fanStatusResolve(fanStatus) {
        var elms = ["#fast", "#normal", "#low"];
        //去除样式
        for (var i = 0; i < elms.length; i++) {
            $(elms[i]).removeClass("active");
        }
        //添加样式
        for (i = 0; i < fanStatus.length; i++) {
            if ((i < 3) && (fanStatus[i] != "0")) {
                $(elms[i]).addClass("active");
            }
        }
    }

    //解析空调运行状态
    function airConditionerRunStResolve(airConditionerRunSt) {
        var elms = ["#cold1", "#cold2", "#hot1", "#hot2", "#clear", "#new", "#shuang1", "#shuang2"];
        //去除样式
        for (var i = 0; i < elms.length; i++) {
            $(elms[i]).removeClass("active");
        }
        //添加样式
        for (i = 0; i < airConditionerRunSt.length; i++) {
            if (airConditionerRunSt[i] != "0") {
                $(elms[i]).addClass("active");
            }
        }
    }

    function getTipStr(faults,key){
        var result="";
        if(!$.isEmptyObject(faults)){
            for(var i=0;i<faults.length;i++){
                var fault = faults[i];
                if(fault.key===key){
                    return fault.value;
                }
            }
        }
        return result;
    }

    gotoSearch = function(carId){
    	//切换成线路
		commonFun.showLeftTree(commonFun.carTree);
		//勾选并展开节点
		commonFun.setSelectedNodes(carId);
    };

    //加载故障类型
    var req = $.ajaxExt({
        url: '/airConditioner/getTCommMftCopaAir.htm',
        success: function (result) {
            tips = result || {};
        }
    });
    
});